package com.LeeCode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 二叉搜索树的最小绝对差
 */

public class Code530 {
    public static void main(String[] args) {
        Integer[] arr = {236, 104, 701, null, 227, null, 911};
        TreeNode root = Utils.buildTree(arr);
        System.out.println(new Code530().getMinimumDifference(root));
    }

    public int getMinimumDifference(TreeNode root) {
        int ans = Integer.MAX_VALUE;

        TreeNode node = root, prev = null;
        Deque<TreeNode> stack = new LinkedList<>();
        while (node != null || !stack.isEmpty()) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            if (prev != null) {
                ans = Math.min(ans, node.val - prev.val);
            }
            prev = node;
            node = node.right;
        }
        return ans;
    }
}
